/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.LoaiNhanVien;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author Administrator
 */
public class LoaiNhanVienDAO {
    /**
     * @desc lay danh sach loai nhan vien
     * @return 
     */
    public static List<LoaiNhanVien> layDanhSach() {
        List<LoaiNhanVien> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select nv from LoaiNhanVien nv";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim loai nhan vien theo ma loai nhan vien
     * @param maLoaiNhanVien
     * @return 
     */
    public static LoaiNhanVien layThongTinLoaiNhanVien(int maLoaiNhanVien) {
        LoaiNhanVien nv = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            nv = (LoaiNhanVien) session.get(LoaiNhanVien.class, maLoaiNhanVien);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return nv;
    }
    
    /**
     * @desc them loai nhan vien
     * @param nv
     * @return 
     */
    public static boolean themLoaiNhanVien(LoaiNhanVien nv) {
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (LoaiNhanVienDAO.layThongTinLoaiNhanVien(nv.getMaLoaiNv())!=null) { 
            return false; 
        } 
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.save(nv); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return true;
    }
    
    /**
     * @desc cap nhat thong tin loai nhan vien
     * @param nv
     * @return 
     */
    public static boolean capNhatThongTinLoaiNhanVien(LoaiNhanVien nv) { 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (LoaiNhanVienDAO.layThongTinLoaiNhanVien(nv.getMaLoaiNv()) == null) { 
            return false; 
        } 
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(nv); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return true; 
    } 
    
    /**
     * @desc xoa loai nhan vien
     * @param maLoaiNhanVien
     * @return 
     */
    public static boolean xoaLoaiNhanVien(int maLoaiNhanVien) { 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        LoaiNhanVien nv = LoaiNhanVienDAO.layThongTinLoaiNhanVien(maLoaiNhanVien); 
        if(nv==null){ 
            return false; 
        } 
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(nv); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return true; 
    }
    
}
